package com.tzq.SERVLET;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tzq.JAVABEAN.ConnectDB;

/**
 * Servlet implementation class CheckData
 */
public class CheckData extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CheckData() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		boolean flag=false;
		String sql="";
		boolean[] watchInt={};
		String[] SqlParam ={};
		PrintWriter out = response.getWriter();
		int method = Integer.parseInt(request.getParameter("method"));
		if(method==0){
			String usernameString = request.getParameter("RegistUserName");
			sql = "select * from Userinfo where UserName=?";
			String[] tempParam={usernameString};
			SqlParam=tempParam;
			boolean[] tempint = {false};
			watchInt = tempint;
		}
		if(method==1){
			String useremail = request.getParameter("email");
			sql="select * from Userinfo where Email=?";
			String[] tempParam={useremail};
			SqlParam=tempParam;
			boolean[] tempint = {false};
			watchInt = tempint;
		}
		if(method==2){
			String useridString = request.getSession().getAttribute("pid").toString();
			String albumnameString  = request.getParameter("albumname");
			sql="select * from ImageAlbum where UserID=? and AlbumName=?";
			String[] tempParam={useridString,albumnameString};
			SqlParam = tempParam;
			boolean[] tempint = {true,false};
			watchInt = tempint;
		}
		if(method==3){
			String useridString = request.getSession().getAttribute("pid").toString();
			String titleString  =request.getParameter("title");
			sql="select * from Blog where AuthorID=? and BlogName=?";
			String[] tempParam = {useridString,titleString};
			SqlParam=tempParam;
			boolean[] tempint ={true,false};
			watchInt=tempint;
		}
		try {
			ConnectDB connectDB = new ConnectDB();
	 		ResultSet rsResultSet = connectDB.executeQuery(sql, SqlParam,watchInt);
	 		while(rsResultSet.next()){
			     if(rsResultSet.getString(1)!=null&&rsResultSet.getString(1).length()!=0){
				       flag = true;
			     }
		    }
		} catch (Exception e) {
			e.printStackTrace();
		}
 		if(flag){
 			out.print("have data");
 		}else{
 			out.print("no data");
 		}
	}
	
}
